package servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import DAO.ZCDJ;
import java.util.List;
/**
 * Servlet implementation class addAssetServlet
 */
public class addAssetServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public addAssetServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	    request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/plain");
		PrintWriter out = response.getWriter();   
		
		String asset[] = new String[10];  //资产登记变量的数组

		/*
		 * 资产信息的参数
		 */
	    asset[0] = request.getParameter("AssetNo");
	    asset[1] = request.getParameter("AssetName");
	    asset[2] = request.getParameter("AssetBarcode");
	    asset[3] = request.getParameter("AssetState");
	    asset[4] = request.getParameter("BelongTo");
	    asset[5] = request.getParameter("Comment");
	    asset[6] = request.getParameter("Sku");
	    asset[7] = request.getParameter("DeviceType");
	    /*asset[6] = request.getParameter("Principal");
	    asset[7] = request.getParameter("ProduceUnit");
	    asset[8] = request.getParameter("Depreciation");
	    asset[9] = request.getParameter("AppertainEquipment");
	    asset[10] = request.getParameter("EquipNumber");
	    asset[11] = request.getParameter("Model");
	    asset[12] = request.getParameter("Type");
	    asset[13] = request.getParameter("ProduceDate");
	    asset[14] = request.getParameter("Money");
	    asset[15] = request.getParameter("Department");
	    asset[16] = request.getParameter("BuildCharge");
	    asset[17] = request.getParameter("StartUseTime");
	    asset[18] = request.getParameter("UsedYears");
	    asset[19] = request.getParameter("LifeYears");
	    asset[20] = request.getParameter("remark");*/
	    
	    System.out.println("--------------------- BelongTo" + asset[4]);
	    
	    /*
	     * 调用DAO里的addAsset函数，将相关资产信息写入数据库
	     * 数组参数asset
	     */
	    ZCDJ zcdj = new ZCDJ();
	    List list = zcdj.getAssetByQuery(0, 20, "", "",asset[2], "", "", "", "","");
	    //如果barcode已经存在，则禁止添加
	    if(list.size()>0)
	    {
	    	out.print("{success:true, msg:'The SN already exists'}"); //应答
	    }
	    else{
	    	zcdj.addAsset(asset);
	    	out.print("{success:true,msg:'succeed'}"); //应答
	    }
	}

}
